package rpc01;

import com.hl.rpc.common.User;

import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;

/**
 * Created by yuanhailong on 2021/10/30.
 */
public class Client {


    public static void main(String[] args) throws Exception{
        Socket socket=new Socket("localhost",9999);

        System.out.println("rpc01 客户端启动...");

        /**
         * 往服务端写入数据
         */
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        DataOutputStream dos=new DataOutputStream(baos);
        dos.writeInt(111);
        socket.getOutputStream().write(baos.toByteArray());
        socket.getOutputStream().flush();


        /**
         * 获取服务端数据
         */
        DataInputStream dis=new DataInputStream(socket.getInputStream());
        int id=dis.readInt();
        String name=dis.readUTF();
        User user=new User(id,name);

        System.out.println("客户端接受服务端返回消息:"+user.toString());


        dos.close();
        socket.close();

    }
}
